package cc.iotkit.data.service;

import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.common.utils.StringUtils;
import cc.iotkit.data.dao.BasAreaRepository;
import cc.iotkit.data.dao.IJPACommData;
import cc.iotkit.data.model.TbBasArea;
import cc.iotkit.data.model.TbSysDictType;
import cc.iotkit.data.system.IBasAreaData;
import cc.iotkit.data.util.PredicateBuilder;
import cc.iotkit.model.bas.BasAreaRecord;
import cc.iotkit.model.system.SysDictType;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static cc.iotkit.data.model.QTbBasArea.tbBasArea;
import static cc.iotkit.data.model.QTbSysDictType.tbSysDictType;

@Primary
@Service
public class BasAreaDataImpl implements IBasAreaData, IJPACommData<BasAreaRecord, Long> {
    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Autowired
    private BasAreaRepository basAreaRepository;

    @Override
    public JpaRepository getBaseRepository() {
        return basAreaRepository;
    }

    @Override
    public Class getJpaRepositoryClass() {
        return TbBasArea.class;
    }

    @Override
    public Class getTClass() {
        return BasAreaRecord.class;
    }

    public List<BasAreaRecord> findByConditions(BasAreaRecord record) {
        List<TbBasArea> rets=jpaQueryFactory.select(tbBasArea).from(tbBasArea)
                .where(PredicateBuilder.instance()
                        .and(Objects.nonNull(record.getId()),()->tbBasArea.id.eq(record.getId()))
                        .and(Objects.nonNull(record.getParentId()),()->tbBasArea.parentId.eq(record.getParentId()))
                        .and(StringUtils.isNotEmpty(record.getAreaName()),()->tbBasArea.areaName.like("%"+record.getAreaName()+"%"))
                        .build()).fetch();
        return MapstructUtils.copyListToList(rets, BasAreaRecord.class);
    }
}
